package com.sso.auth.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sso.auth.entity.User;
import com.sso.auth.mapper.UserMapper;
import com.sso.auth.service.UserService;
import com.sso.common.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 用户服务实现类
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    
    private final UserMapper userMapper;
    
    @Override
    public User findByUsername(String username) {
        if (StrUtil.isBlank(username)) {
            return null;
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        return userMapper.selectOne(wrapper);
    }
    
    @Override
    public User findByEmail(String email) {
        if (StrUtil.isBlank(email)) {
            return null;
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, email);
        return userMapper.selectOne(wrapper);
    }
    
    @Override
    public User findByPhone(String phone) {
        if (StrUtil.isBlank(phone)) {
            return null;
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, phone);
        return userMapper.selectOne(wrapper);
    }
    
    @Override
    public User createUser(User user) {
        // 检查用户名是否已存在
        if (findByUsername(user.getUsername()) != null) {
            throw new BusinessException("用户名已存在");
        }
        
        // 检查邮箱是否已存在
        if (StrUtil.isNotBlank(user.getEmail()) && findByEmail(user.getEmail()) != null) {
            throw new BusinessException("邮箱已存在");
        }
        
        // 检查手机号是否已存在
        if (StrUtil.isNotBlank(user.getPhone()) && findByPhone(user.getPhone()) != null) {
            throw new BusinessException("手机号已存在");
        }
        
        // 加密密码
        user.setPassword(BCrypt.hashpw(user.getPassword()));
        user.setStatus(1);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        
        userMapper.insert(user);
        return user;
    }
    
    @Override
    public User updateUser(User user) {
        User existingUser = userMapper.selectById(user.getId());
        if (existingUser == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 检查用户名是否已被其他用户使用
        User userByUsername = findByUsername(user.getUsername());
        if (userByUsername != null && !userByUsername.getId().equals(user.getId())) {
            throw new BusinessException("用户名已存在");
        }
        
        // 检查邮箱是否已被其他用户使用
        if (StrUtil.isNotBlank(user.getEmail())) {
            User userByEmail = findByEmail(user.getEmail());
            if (userByEmail != null && !userByEmail.getId().equals(user.getId())) {
                throw new BusinessException("邮箱已存在");
            }
        }
        
        // 检查手机号是否已被其他用户使用
        if (StrUtil.isNotBlank(user.getPhone())) {
            User userByPhone = findByPhone(user.getPhone());
            if (userByPhone != null && !userByPhone.getId().equals(user.getId())) {
                throw new BusinessException("手机号已存在");
            }
        }
        
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
        return user;
    }
    
    @Override
    public void deleteUser(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        userMapper.deleteById(id);
    }
    
    @Override
    public void updateLastLoginInfo(Long userId, String loginIp) {
        User user = new User();
        user.setId(userId);
        user.setLastLoginTime(LocalDateTime.now().toString());
        user.setLastLoginIp(loginIp);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
    }
} 